<template>
  <div :class="{ 'actionSheet-container': true, 'active': isActive }" v-if="show" @click="hide">
    <div :class="{ 'actionSheet': true, 'active': isActive }">
      <span class="actionSheet-title" v-if="title">{{ title }}</span>
      <ul class="actionSheet-list">
        <li
          class="actionSheet-item"
          v-for="(item,index) in itemList"
          :key="item"
          @click.stop="itemClick(index)"
        >{{ item }}</li>
      </ul>
      <span class="actionSheet-cancel" @click.stop="cancelClick">取消</span>
    </div>
  </div>
</template>
<script>
export default {
  name: "actionSheet",
  data() {
    return {
      isActive: false,
      timeInterval: 250,
      show: true,
      title: "",
      itemList: [],
      itemFunction: function() {},
      cancelFunction: function() {}
    };
  },
  mounted() {
    setTimeout(() => {
      this.isActive = true;
    }, 100);
  },
  methods: {
    itemClick(index) {
      this.hide();
      if (this.itemFunction) this.itemFunction(index);
    },
    cancelClick() {
      this.hide();
      if (this.cancelFunction) this.cancelFunction();
    },
    hide() {
      this.isActive = false;
      setTimeout(() => {
        this.show = false;
      }, this.timeInterval);
    }
  }
};
</script>
<style scoped>
.actionSheet-container {
  z-index: 100;
  position: fixed;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  opacity: 0;
  transition: opacity 0.25s ease-out;
}
.actionSheet-container.active {
  opacity: 1;
}
.actionSheet {
  position: absolute;
  left: 0;
  bottom: 0;
  transform: translateY(100%);
  width: 100%;
  background-color: #eee;
  transition: all 0.25s ease-out;
}
.actionSheet.active {
  transform: translateY(0);
}
.actionSheet-title {
  display: block;
  text-align: center;
  font-weight: bold;
  font-size: 0.4rem;
  height: 1.066667rem;
  line-height: 1.066667rem;
  background-color: white;
}
.actionSheet-list {
  background-color: white;
}
.actionSheet-item {
  display: block;
  width: 100%;
  line-height: 1.066667rem;
  text-align: center;
  border-top: 1px solid #eee;
  font-size: 0.333333rem;
  color: #fe0000;
}
.actionSheet-cancel {
  display: block;
  margin-top: 0.266667rem;
  line-height: 1.066667rem;
  text-align: center;
  font-size: 0.333333rem;
  background-color: white;
}
</style>